﻿using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.Base.DB
{
    public class DB_Config : BaseCacheML<Frame_Config>
    {

        public override void Insert(Frame_Config Entity)
        {
            if (string.IsNullOrEmpty(Entity.ConfigGuid))
            {
                Entity.ConfigGuid = GenerateUtil.GenerateRowGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
        }

        public override void InsertBatch(List<Frame_Config> EntityList)
        {
            foreach (Frame_Config Entity in EntityList)
            {
                if (string.IsNullOrEmpty(Entity.ConfigGuid))
                {
                    Entity.ConfigGuid = GenerateUtil.GenerateRowGuid();
                }
                Entity.AddDate = DateTime.Now;
            }
            base.InsertBatch(EntityList);
        }

        public Page SelectPage(string ConfigName, string GroupGuid)
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<Frame_Config> entityList = DTO.Select_Cache<Frame_Config>();
            int total = entityList.Count;
            if (!string.IsNullOrEmpty(ConfigName))
            {
                entityList = entityList.FindAll((Frame_Config item) => item.ConfigName.Contains(ConfigName));
            }
            if (!string.IsNullOrEmpty(GroupGuid) && GroupGuid != "Top")
            {
                entityList = entityList.FindAll((Frame_Config item) => item.GroupGuid == GroupGuid);
            }
            entityList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<Frame_Config> resultList = new List<Frame_Config>();
            for (int i = (PageIndex - 1) * PageSize; i < entityList.Count && i < PageIndex * PageSize; i++)
            {
                resultList.Add(entityList[i]);
            }
            Page page = new Page();
            page.Rows = resultList;
            page.Total = total;
            return page;
        }

        public bool ExistConfig(string ConfigName)
        {
            List<Frame_Config> entityList = DTO.Select_Cache<Frame_Config>();
            entityList = entityList.FindAll((Frame_Config item) => item.ConfigName == ConfigName);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ExistConfig(string ConfigGuid,string ConfigName)
        {
            List<Frame_Config> entityList = DTO.Select_Cache<Frame_Config>();
            entityList = entityList.FindAll((Frame_Config item) => item.ConfigName == ConfigName && item.ConfigGuid != ConfigGuid);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Frame_Config SelectConfigByName(string ConfigName )
        {
            List<Frame_Config> entityList = DTO.Select_Cache<Frame_Config>();
            entityList = entityList.FindAll((Frame_Config item) => item.ConfigName == ConfigName);
            if (entityList.Count > 0)
            {
                return entityList[0];
            }
            else
            {
                return null;
            }
        }
    }
}
